package com.shujia.flink.tf;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class Demo2FlatMap {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> linesDS = env.socketTextStream("master", 8888);

        //1、使用匿名内部类的方式
        DataStream<String> wordsDS = linesDS
                .flatMap(new FlatMapFunction<String, String>() {
                    @Override
                    public void flatMap(String line, Collector<String> out) throws Exception {
                        for (String word : line.split(",")) {
                            //循环将数据发送到下游
                            out.collect(word);
                        }
                    }
                });

        //wordsDS.print();


        //lambda表达式
        linesDS.flatMap((line, out) -> {
            for (String word : line.split(",")) {
                //循环将数据发送到下游
                out.collect(word);
            }
        }, Types.STRING).print();


        env.execute();

    }
}
